﻿/**
 * Copyright (c) 2009, Carlos Mirabella
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Carlos Mirabella nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 **/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DineroMail.IPN.Reporte
{
    public class Operacion
    {

        public enum EstadoEnum
        {
            PendienteDePago=1,
            Acreditado=2,
            Cancelado=3
        }
        public enum MetodoPagoEnum
        {
            FondosDineroMail=1,
            Efectivo=2,
            TarjetaCredito=3,
            TransferenciaBancaria=4
        }
        #region Variables
        private String id;
        private String fecha;
        private EstadoEnum estado;
        private String numTransaccion;
        private Comprador comprador;
        private String monto;
        private String montoNeto;
        private MetodoPagoEnum metodoPago;
        private String medioPago;
        private String cuotas;
        private List<Item> items;
        private Vendedor vendedor;
        #endregion
       
        #region Getters y Setters
        public String Id
        {
            get { return id; }
            set { id = value; }
        }
        public String Fecha
        {
            get { return fecha; }
            set { fecha = value; }
        }
        public EstadoEnum Estado
        {
            get { return estado; }
            set { estado = value; }
        }
        public String NumTransaccion
        {
            get { return numTransaccion; }
            set { numTransaccion = value; }
        }
        public Comprador Comprador
        {
            get { return comprador; }
            set { comprador = value; }
        }
        public String Monto
        {
            get { return monto; }
            set { monto = value; }
        }
        public String MontoNeto
        {
            get { return montoNeto; }
            set { montoNeto = value; }
        }
        public MetodoPagoEnum MetodoPago
        {
            get { return metodoPago; }
            set { metodoPago = value; }
        }
        public String MedioPago
        {
            get { return medioPago; }
            set { medioPago = value; }
        }
        public String Cuotas
        {
            get { return cuotas; }
            set { cuotas = value; }
        }
        public List<Item> Items
        {
            get { return items; }
            set { items = value; }
        }
        public Vendedor Vendedor
        {
            get { return vendedor; }
            set { vendedor = value; }
        }
        #endregion

        public Operacion()
        {
        }


        public static Operacion GetOperacion(XmlNode parent)
        {
            Operacion oper = new Operacion();
            foreach (XmlNode child in parent.ChildNodes)
            {
                if (child.Name.Equals("ID"))
                    oper.Id = child.FirstChild.Value;
                else if (child.Name.Equals("FECHA"))
                    oper.Fecha = child.FirstChild.Value;
                else if (child.Name.Equals("ESTADO"))
                    oper.Estado = (EstadoEnum)Enum.Parse(typeof(EstadoEnum),child.FirstChild.Value);
                else if (child.Name.Equals("NUMTRANSACCION"))
                    oper.NumTransaccion = child.FirstChild.Value;
                else if (child.Name.Equals("COMPRADOR"))
                    oper.Comprador = Comprador.GetComprador(child);
                else if (child.Name.Equals("MONTO"))
                    oper.Monto = child.FirstChild.Value;
                else if (child.Name.Equals("MONTONETO"))
                    oper.MontoNeto = child.FirstChild.Value;
                else if (child.Name.Equals("METODOPAGO"))
                    oper.MetodoPago = (MetodoPagoEnum)Enum.Parse(typeof(MetodoPagoEnum),child.FirstChild.Value);
                else if (child.Name.Equals("MEDIOPAGO"))
                    oper.MedioPago = child.FirstChild.Value;
                else if (child.Name.Equals("CUOTAS"))
                    oper.Cuotas = child.FirstChild.Value;
                else if (child.Name.Equals("ITEMS"))
                    oper.Items = Operacion.GetItems(child);
                else if (child.Name.Equals("VENDEDOR"))
                    oper.Vendedor = Vendedor.GetVendedor(child);

            }
            return oper;
        }
        
        private static List<Item> GetItems(XmlNode parent)
        {
            List<Item> items = new List<Item>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                if (child.Name.Equals("ITEM"))
                {
                    items.Add(Item.GetItem(child));
                }
            }
            return items;
        }

    }
}
